import Vue from 'vue'
import Vuex from 'vuex'
import {
  setLocalInfo,
  removeLocalInfo,
  getLocalInfo,
  setUser,
  getUser,
  removeUser
} from '@/utils/auth'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: JSON.parse(getUser()),
    cookie: getLocalInfo('cookie'),
    img: getLocalInfo('Img'),
    uid: getLocalInfo('uid')
  },
  mutations: {
    // 设置用户信息
    setUserInfo(state, data) {
      state.userInfo = data
      setUser(data)
    },
    // 设置Cookie并本地储存
    setCookie(state, data) {
      state.cookie = data
      setLocalInfo('cookie', data)
    },
    // 设置uid并本地储存
    setUid(state, uid) {
      state.uid = uid
      setLocalInfo('uid', uid)
    },
    // 删除用户信息
    delUserInfo(state) {
      state.userInfo = {}
      removeUser()
    },
    // 删除Cookie
    removeCookie(state) {
      state.cookie = ''
      removeLocalInfo('cookie')
    },
    setImg(state, data) {
      state.img = data
      setLocalInfo('Img', data)
    },
    removeImg(state) {
      state.img = ''
      removeLocalInfo('Img')
    },
    // 删除uid
    removeUid(state) {
      state.uid = ''
      removeLocalInfo('uid')
    }
  },
  actions: {
    getUserInfo(contant, data) {
      contant.commit('setUserInfo', data)
    },
    getCookie(contant, cookie) {
      contant.commit('setCookie', cookie)
    },
    getImg(contant, data) {
      contant.commit('setImg', data)
    },
    getUid(contant, uid) {
      contant.commit('setUid', uid)
    }
  },
  modules: {}
})
